package com.dc.dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import com.dc.dao.ShopDao;
import com.dc.entity.Shop;
import com.dc.tools.DBTools;
import com.dc.tools.PageData;

public class ShopDaoImpl implements ShopDao {

	@Override
	public boolean addShop(Shop shop) {
		if (shop != null) {
			String sql = "insert into shop values(null,?,?,?,?,?,?,?,?,?,?)";
			int n = DBTools.exUpdate(sql, shop.getShopname(), shop.getShoppsw(), shop.getShopphone(),
					shop.getShopemail(), shop.isShopstate(), shop.getShopaddress(), shop.getShopdianming(),
					shop.getShopdate(), shop.getShopimage(), shop.getShoptime());
			return n > 0;
		}
		return false;
	}

	@Override
	public boolean deleteShop(int shopid) {
		// TODO Auto-generated method stub
		String sql = "delete from shop where shopid=?";
		int n = DBTools.exUpdate(sql, shopid);
		return n > 0;
	}

	@Override
	public boolean updateShop(Shop shop) {
		// TODO Auto-generated method stub
		if (shop != null) {
			String sql = "update shop set shopname=?,shoppsw=?,shopphone=?,shopemail=?,shopstate=?,shopaddress=?,shopdianming=?,shopdate=?,shopimage=?,shoptime=? where shopid=?";
			int n = DBTools.exUpdate(sql, shop.getShopname(), shop.getShoppsw(), shop.getShopphone(),
					shop.getShopemail(), shop.isShopstate(), shop.getShopaddress(), shop.getShopdianming(),
					shop.getShopdate(), shop.getShopimage(), shop.getShoptime(), shop.getShopid());
			return n > 0;
		}
		return false;

	}

	@Override
	public Object selectShop(String keywords) {
		// TODO Auto-generated method stub
		String sql = "select * from shop where shopdianming like ?";
		Object list = DBTools.exQuery(sql, Shop.class, "%" + keywords + "%");
		return list;
	}

	@Override
	public PageData<Shop> selectShopByPage(int page, int pageSize, String keywords) {
		// TODO Auto-generated method stub
		String sql = "select * from shop where shopdianming like ?";
		PageData<Shop> pd = DBTools.exQueryByPage(sql, Shop.class, page, pageSize, "%" + keywords + "%");
		return pd;
	}


	/**
	 * 登录商家操作的模拟
	 * 
	 * @param name
	 * @param psw
	 * @return true 登录成功 false 登录失败
	 */
	@Override
	public boolean doLogin(String name, String psw) {
		// 完成登录验证的sql语句
		String sql = "select count(shopid) from shop where shopname=? and shoppsw = ?";
		// 得到连接对象
		Connection conn = DBTools.getConn();
		// 创建PreparedStatement对象
		PreparedStatement pst = null;
		ResultSet rs = null;
		int n = 0;

		try {
			pst = conn.prepareStatement(sql);
			// 设置？的值
			pst.setString(1, name);
			pst.setString(2, psw);
			// 执行sql
			rs = pst.executeQuery();
			// 处理结果
			if (rs.next()) {
				n = rs.getInt(1);
			}

		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			// 释放资源
			try {
				rs.close();
				pst.close();
				conn.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}

		}
		return n == 1;
	}

}
